//
//  CYFinanceViewModel.m
//  cyjf
//
//  Created by tauCross on 2017/5/5.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFinanceViewModel.h"



@implementation CYFinanceViewModel

+ (instancetype)modelWithAPIModel:(CYDataAPIFinanceModel *)apiModel
{
    id model = [[self alloc] init];
    ((CYFinanceViewModel *)model).apiModel = apiModel;
    ((CYFinanceViewModel *)model).fid = SAFE_NUMBER(apiModel.id);
    ((CYFinanceViewModel *)model).title = SAFE_STRING(apiModel.title);
    ((CYFinanceViewModel *)model).rate = [NSString stringWithFormat:@"%.2f", SAFE_NUMBER(apiModel.userInterest).doubleValue];
    ((CYFinanceViewModel *)model).additionRate = apiModel.extraInterest.doubleValue > 0 ? [NSString stringWithFormat:@"%.2f", SAFE_NUMBER(apiModel.extraInterest).doubleValue] : nil;
    ((CYFinanceViewModel *)model).due = [NSString stringWithFormat:@"%li", (long)(SAFE_NUMBER(apiModel.duration).integerValue)];
    ((CYFinanceViewModel *)model).perCent = [NSString stringWithFormat:@"%li", (long)(SAFE_NUMBER(apiModel.percent).integerValue)];
    ((CYFinanceViewModel *)model).tag = SAFE_STRING(apiModel.tag);
    switch(apiModel.status.integerValue)
    {
        case 2:
        {
            NSDate *date = [NSDate date];
            NSDate *serverDate = [NSDate dateWithTimeIntervalSince1970:SAFE_NUMBER(apiModel.currentSystemTime).doubleValue / 1000.0];
            NSTimeInterval dValue = [date timeIntervalSinceDate:serverDate];
            NSTimeInterval start = SAFE_NUMBER(apiModel.startTime).doubleValue / 1000.0 + dValue;
            ((CYFinanceViewModel *)model).saleDate = [NSDate dateWithTimeIntervalSince1970:start];
            
            if([((CYFinanceViewModel *)model).saleDate timeIntervalSinceDate:date] <= 0)
            {
                ((CYFinanceViewModel *)model).status = CYFinanceViewModelStatusOnSale;
            }
            else
            {
                ((CYFinanceViewModel *)model).status = CYFinanceViewModelStatusWillSale;
            }
            
            break;
        }
        case 3:
        case 4:
        {
            ((CYFinanceViewModel *)model).status = CYFinanceViewModelStatusFull;
            break;
        }
        case 5:
        {
            ((CYFinanceViewModel *)model).status = CYFinanceViewModelStatusDone;
            break;
        }
        default:
        {
            break;
        }
    }
    return model;
}

@end
